﻿using System;
using System.ComponentModel;

namespace Chapter04
{
    //可控类型装箱和拆箱
    [Description("Listing 4.2")]
    class BoxingAndUnboxing
    {
        static void Main()
        {
            Nullable<int> nullable = 5;

            object boxed = nullable;//装箱成有值的可空类型
            Console.WriteLine(boxed.GetType());

            int normal = (int)boxed;
            Console.WriteLine(normal);//拆箱成普通类型

            nullable = (Nullable<int>)boxed;//拆箱成可空类型
            Console.WriteLine(nullable);

            nullable = new Nullable<int>();
            boxed = nullable; //装箱成没有值的可空类型
            Console.WriteLine(boxed == null);

            nullable = (Nullable<int>)boxed;//拆箱成没有值得可空类型
            Console.WriteLine(nullable.HasValue);

            //normal = (Nullable<int>)boxed;//没有值的boxed引用类型不能拆箱成普通的int类型
        }
    }
}
